package pers.byxiaobai.levelhealth.listeners;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLevelChangeEvent;

import pers.byxiaobai.levelhealth.config.ConfigData;
import pers.byxiaobai.levelhealth.data.PlayerDataManager;
import pers.byxiaobai.levelhealth.utils.PlayerHealthUtil;

/** 
* @author byxiaobai
* Listener
*/
public class PlayerLevelChangeListener implements Listener{
	
	@EventHandler
    public void onPlayerLevelChange(PlayerLevelChangeEvent evt) {
		Player player=evt.getPlayer();
		int lastLevel=evt.getOldLevel();
		int newLevel=evt.getNewLevel();
		if(newLevel>lastLevel) {
			PlayerDataManager.INSTANCE.setIsAddedHealth(player, false);
			if(ConfigData.INSTANCE.IS_RESET_HEALTH_ON_LEVEL_UP)//如果升级重置血量
				player.setHealth(PlayerHealthUtil.getPlayerMaxHealth(player));
			return;
		}
		PlayerDataManager.INSTANCE.setIsAddedHealth(player, false);
	}
}
